package com.nsgf.library.ui.mine;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.PointListBean;
import com.nsgf.library.data.bean.PointListBean.ListBean;
import com.nsgf.library.data.bean.UserDataBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ActivityMineRedPointsBinding;
import com.zhht.htzx.databinding.ItemMinePointsRedBinding;

import java.util.ArrayList;
import java.util.List;


/**
 * 分红点
 */
public class MineRedPointsActivity extends BaseActivity {

    private RedPointStates mStates;

    private SimpleDataBindingAdapter<ListBean, ItemMinePointsRedBinding> mAdapter;

    private AccountRequester mAccountRequester;

    private int index;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(RedPointStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<ListBean, ItemMinePointsRedBinding>(this, R.layout.item_mine_points_red,
                new DiffUtil.ItemCallback<ListBean>() {
                    @Override
                    public boolean areItemsTheSame(@NonNull ListBean oldItem, @NonNull ListBean newItem) {
                        return oldItem.getId() == newItem.getId();
                    }

                    @Override
                    public boolean areContentsTheSame(@NonNull ListBean oldItem, @NonNull ListBean newItem) {

                        if (oldItem.getId() != newItem.getId())
                            return false;
                        if (!oldItem.getCreateTime().equals(newItem.getCreateTime()))
                            return false;
                        if (!oldItem.getTitle().equals(newItem.getTitle()))
                            return false;
                        if (!oldItem.getDescription().equals(newItem.getDescription()))
                            return false;
                        if (oldItem.getPoint() != newItem.getPoint())
                            return false;

                        return true;
                    }
                }) {
            @Override
            protected void onBindItem(ItemMinePointsRedBinding binding, ListBean item,
                                      RecyclerView.ViewHolder holder) {
                binding.setVm(item);

            }
        };
        return new DataBindingConfig(R.layout.activity_mine_red_points, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    private ActivityMineRedPointsBinding mBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityMineRedPointsBinding) getBinding();
        getLifecycle().addObserver(mAccountRequester);
        mStates.titleBar.set(getIntent().getStringExtra("title"));
        index = getIntent().getIntExtra("index", 2);
        mAccountRequester.getPointDetail();
        mAccountRequester.getUserResultProject().observe(this, new Observer<ResponseStatus<UserDataBean>>() {
            @Override
            public void onChanged(ResponseStatus<UserDataBean> response) {
                if (response.getResponseCode() == 0) {
                    mStates.userDataBeanState.set(response.getData());
                    if (index == 2) {
                        mBinding.tvMinePointTitle.setText("当前分红点");
                        mBinding.tvMinePointValue.setText(mStates.userDataBeanState.get().getDividendPoint());
                        mBinding.tvMinePointFreezeValue.setText("");
                    } else if (index == 8) {
                        mBinding.tvMinePointTitle.setText("当前爱心基金");
                        mBinding.tvMinePointValue.setText(mStates.userDataBeanState.get().getLoveFund());
                        mBinding.tvMinePointFreezeValue.setText("");
                    } else {
                        mBinding.tvMinePointTitle.setText("当前贡献值");
                        mBinding.tvMinePointValue.setText(mStates.userDataBeanState.get().getContributionSrc());
                        mBinding.tvMinePointFreezeValue.setText(mStates.userDataBeanState.get().getFreezeContributionSrc());
                    }
                }
            }
        });
        mAccountRequester.getPointBeanResult().observe(this, new Observer<ResponseStatus<PointListBean>>() {
            @Override
            public void onChanged(ResponseStatus<PointListBean> response) {
                if (response.getResponseCode() == 0) {

                    if (page == 1) {
                        mBinding.swipeRefreshLayout.finishRefresh();
                        mStates.pointsList.set(response.getData().getList());
                        if (!mStates.pointsList.get().isEmpty())
                            mBinding.ivListNotData.setVisibility(GONE);
                        else mBinding.ivListNotData.setVisibility(VISIBLE);
                    } else {
                        mBinding.swipeRefreshLayout.finishLoadMore();

                        List<PointListBean.ListBean> currentList = mStates.pointsList.get();
                        if (currentList != null) {
                            currentList.addAll(response.getData().getList());
                            mStates.pointsList.set(currentList);
                        }
                    }
                }
            }
        });


        mBinding.swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getDatas();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getDatas();
            }
        });

        mBinding.swipeRefreshLayout.autoRefresh();


    }


    private int page = 1;

    private void getDatas() {
        mAccountRequester.getPointsBenefit(page, index);
    }

    public static class RedPointStates extends StateHolder {

        public final State<List<ListBean>> pointsList = new State<>(new ArrayList<>());
        public final State<UserDataBean> userDataBeanState = new State<>(new UserDataBean());
    }

}
